<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
use Mail;

class UserController extends Controller
{
    public function personal()
    {
    	$res = DB::table('userdetail') -> where('id', session('user') -> id) -> first();
    	if($res)
    	{
            if(!empty($res -> abde))
            {
                $res -> abde = explode(',', $res -> abde);
            }
            if(!empty($res -> birthday))
            {
                $res -> birthday = explode('-', $res -> birthday);
            }
    		return view('home.user.personal', ['data' => $res]);
    	}
    }

    public function update(Request $request)
    {
    	$data = $request -> except('_token');
    	$data['abde'] = $data['s_province'].','.$data['s_city'].','.$data['s_county'];
    	$data['birthday'] = $data['y'].'-'.$data['m'].'-'.$data['d'];
    	if($data['s_province'] == '省份')
    	{
    		unset($data['abde']);
    		unset($data['s_province']);
    		unset($data['s_county']);
    		unset($data['s_city']);
    	}else
        {
            unset($data['s_province']);
            unset($data['s_city']);
            unset($data['s_county']);
        }
        $id = $data['id'];

        unset($data['id']);
    	unset($data['y']);
    	unset($data['m']);
    	unset($data['d']);
    	$res = Db::table('userdetail') -> where('id', $id) -> update($data);
    	if($res)
    	{
            session('user') -> name = $data['name'];
    		return redirect('/home/user/personal') -> with(['info' => '更新成功']);
    	}else
    	{
    		return back() -> with(['info' => '更新失败']);
    	}
    }

    public function photo()
    {
    	$res = DB::table('userdetail') -> where('id', session('user') -> id) -> first();
    	return view('home.user.photo', ['data' => $res]);
    }

    public function dophoto(Request $request)
    {
    	$data = $request -> except('_token');

        if ($request -> file('photo') -> isValid())
        {
            $file = $request -> file('photo');
            //允许的mime类型
            $allowMime = array('image/jpeg','image/png','image/gif','image/jpg');
            //获取文件mime类型
            $mime = $file -> getMimeType();
            //判断是否允许
            if(in_array($mime,$allowMime))
            {
                //文件后缀名
                $type = $file -> getClientOriginalExtension($file);
                //文件新名字
                $newName = md5(rand(10000,99999)).time().'.'.$type;
                //保存文件
                $file -> move('./images/poster/', $newName);
                
                //存入文件新名字
                $data['photo'] = '/images/poster/'.$newName;
                //存入数据库
                $res = DB::table('userdetail') -> where('id', $data['id']) -> update($data);

                if($res)
                {
                    session('user') -> photo = $data['photo'];
                    return redirect('/home/user/photo') -> with(['info' => '更换成功']);
                }else
                {
                    return back() -> with(['info' => '更换失败']);
                }

            }else
            {
                return back() -> with(['info' => '更换失败']);
            }
            
        }else
        {
            return back() -> with(['info' => '更换失败']);
        }
    }

    //修改密码
    public function upwd()
    {
        return view('home.user.upwd');
    }

    public function newpwd(Request $request)
    {
        $data = $request -> except('_token');
        $res = DB::table('users') -> where('id',$data['id']) -> first();
        if($res)
        {
            $opwd = Crypt::decrypt($res -> password);
            if($data['oldpassword'] == $opwd)
            {
                $newpwd = Crypt::enCrypt($data['password']);
                $rew = DB::table('users') -> where('id', $data['id']) -> update(['password' => $newpwd]);

                if($rew)
                {
                    return redirect('/home/user/upwd') -> with(['info' => '修改成功']);
                }else
                {
                    return back() -> with(['info' => '修改失败']);
                }
            }else
            {
                return back() -> with(['info' => '修改失败']);
            }
        }else
        {
            return back() -> with(['info' => '修改失败']);
        }
    }

    //找回密码
    public function forget()
    {
        return view('home.user.forget');
    }
    //发送找回密码的邮件
    public function forgetemail(Request $request)
    {
        $data = $request -> except('_token');
        $res = DB::table('users') -> where('email', $data['email']) -> first();
        if(!$res)
        {
            return back() -> with(['info' => '您输入的邮箱不存在']);
        }

        Mail::send('emails.resetPass', ['res' => $res], function($message) use ($res){
            $message -> subject('找回密码');
            $message -> to($res -> email);
        });
            return back() -> with(['info' => '邮件已发送，请在邮箱查看']);
    }
    //newpass
    public function newpass($id, $token)
    {
        //查询
        $res = DB::table('users') -> where('id', $id) -> where('remember_token', $token) -> first();
        if(!$res)
        {
            return view('home.user.info',['info' => '此页面不存在']);
        }else
        {
            return view('home.user.inputpass', ['data' => $res]);
        }
    }
    public function renew(Request $request)
    {
        $data = $request -> except('_token');
        if($data['password'] == $data['towpassword'])
        {
            //加密密码
            $pwd = Crypt::enCrypt($data['password']);
            $res = DB::table('users') -> where('id', $data['id']) -> update(['password' => $pwd]);
            if($res)
            {
                return redirect('/') -> with(['info' => '修改成功']);
            }else
            {
                return back() -> with(['info' => '修改失败']);
            }
        }
    }
}
